<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>vue中的样式绑定</title>
  <style>
    .activated {
      color: red;
    }
  </style>
  <script src="./vue.js"></script>
</head>
<body>
  <div id="root">
    <!-- vue中的样式绑定 :class='{key: val}' -->
    <!-- <div @click="handleDivClick" :class='{actived: isActived}'>hello world</div> -->
    <!-- 第二种实现方式 -->
    <!-- <div @click="handleDivClick" :class='[activated]'>hello world</div> -->
    <!-- 第三种实现方式 -->
    <div @click="handleDivClick" :style='styleObj'>hello world</div>
  </div>

  <script>
    var vm = new Vue({
      el: '#root',
      data: {
        // isActived: false
        // activated: ''
        styleObj: {
          color: 'black'
        }
      },
      methods: {
        handleDivClick: function() { 
          // 点击div，改变class状态，样式有和无来回切换
          // this.isActived = !this.isActived;

          // 第二种实现方式：
          // this.activated = this.activated === 'activated' ? '' : 'activated';
          // if (this.activated === 'activated') {
          //   this.activated = ''
          // } else {
          //   this.activated = 'activated'
          // }

          // 第三种实现方式
          this.styleObj.color = this.styleObj.color === 'black' ? 'red' : "black";
        }
      }
    })
  </script>
</body>
</html>